<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-10-18
 * Time: 16:07
 */
namespace app\index\controller;

use app\index\controller\Base;
use think\Db;
use think\Request;

class Stafflist extends Base
{
    //ajax提交
//    public function index()
//    {
//        $this->baseindex();
//        if (isset($_POST['submit'])) {//搜索功能
//            $results = '';
//            //return 1;
//            $ch_name = input('post.ch_name');
//            $branch = input('post.branch_id');
//            $region = input('post.region_id');
//            //dump($region);
//            //dump($branch);die;
//            $position = input('post.position');
//            if (!empty($region)) {
//                if (!empty($branch)) {
//                    //$result=Db::table('oa_user')->where("branch_id=".$branch )->where("region_id=".$region)->select();
//                    if (!empty($ch_name)) {
//                        if (!empty($position)) {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')->where('u.ch_name', 'like', "%$ch_name%")
//                                ->where('u.position', 'like', "%$position%")->where('u.branch_id=' . $branch)->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        } else {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')->where('u.ch_name', 'like', "%$ch_name%")
//                                ->where('u.branch_id=' . $branch)->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        }
//                    } else {
//                        if (!empty($position)) {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')
//                                ->where('u.position', 'like', "%$position%")->where('u.branch_id=' . $branch)->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        } else {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')
//                                ->where('u.branch_id=' . $branch)->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        }
//                    }
//                } else {
//                    if (!empty($ch_name)) {
//                        if (!empty($position)) {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')->where('u.ch_name', 'like', "%$ch_name%")
//                                ->where('u.position', 'like', "%$position%")->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        } else {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')->where('u.ch_name', 'like', "%$ch_name%")
//                                ->where('u.branch_id=' . $branch)->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        }
//                    } else {
//                        if (!empty($position)) {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')
//                                ->where('u.position', 'like', "%$position%")->where('u.branch_id=' . $branch)->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        } else {
//                            $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')
//                                ->where('u.region_id=' . $region)->paginate(10);
//                            return $results;
//                        }
//                    }
//                }
//            } else {
//                if (!empty($ch_name)) {
//                    if (!empty($position)) {
//                        $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')->where('u.position', 'like', "%$position%")
//                            ->where('u.ch_name', 'like', "%$ch_name%")->paginate(10);
//                        return $results;
//                    } else {
//                        $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')
//                            ->where('u.ch_name', 'like', "%$ch_name%")->paginate(10);
//                        return $results;
//                        //dump($results);die;
//                    }
//                } else {
//                    if (!empty($position)) {
//                        $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')
//                            ->where('u.position', 'like', "%$position%")->paginate(10);
//                        return $results;
//                    } else {
//                        $results = Db::table('oa_user u,oa_branch b,oa_region r')->field('u.id,u.ch_name,u.position,b.branch,r.region')->where('u.branch_id=b.id and u.region_id=r.id')->paginate(10);
//                        return $results;
//                    }
//                }
//            }
//            /*else{
//                if (!empty($branch)){//搜索部门不为空时
//                $result=Db::table('oa_branch')->field('id,branch')->where('branch','like',"%$branch%")->select();
//                if (!empty($result)){
//                    foreach ($result as $kk=>$vv){
//                        $res=$vv['id'];
//                        if (!empty($ch_name)){//搜索姓名不为空时
//                            if (!empty($position)){//搜索职位不为空时
//                                $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.ch_name','like',"%$ch_name%")
//                                    ->where('u.position','like',"%$position%")->where("u.branch_id=b.id and u.branch_id=".$res)->paginate(10);
//                            }else{
//                                $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.ch_name','like',"%$ch_name%")
//                                    ->where("u.branch_id=b.id and u.branch_id=".$res)->paginate(10);
//                            }
//                        }else{
//                            if (!empty($position)){
//                                $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.position','like',"%$position%")
//                                    ->where("u.branch_id=b.id and u.branch_id=".$res)->paginate(10);
//                            }else{
//                                $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.branch_id=b.id and u.branch_id='.$res)->paginate(10);
//                            }
//                        }
//                    }
//                }
//            }else{
//                if (!empty($ch_name)){
//                    if (!empty($position)){
//                        $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.position','like',"%$position%")
//                            ->where('u.ch_name','like',"%$ch_name%")->where('u.branch_id=b.id')->paginate(10);
//                    }else{
//                        $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')
//                            ->where('u.ch_name','like',"%$ch_name%")->where('u.branch_id=b.id')->paginate(10);
//                        //dump($results);die;
//                    }
//                }else{
//                    if (!empty($position)){
//                        $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.position','like',"%$position%")->where('u.branch_id=b.id')->paginate(10);
//                    }else{
//                        $results=Db::table('oa_user u,oa_branch b')->field('u.id,u.ch_name,b.branch,u.position')->where('u.branch_id=b.id')->paginate(10);
//                    }
//                }
//            }
//            }*/
//        } else {
//            $region_result = Db::table('oa_region')->field('id,region')->select();
//            $branch = Db::table('oa_branch')->field('id,branch')->select();
//            //dump($branch);die;
//            // 查询状态为1的用户数据 并且每页显示10条数据
//            $results = Db::table('oa_user u')->field('u.id,u.ch_name,b.branch,u.position,r.region')->join('oa_branch b', 'u.branch_id=b.id', 'left')->join('oa_region r', 'u.region_id=r.id', 'left')->paginate(10);
//            //dump($list);die;
//// 把分页数据赋值给模板变量list
//            $this->assign('list', $results);
//            $this->assign('region', $region_result);
//            $this->assign('branch', $branch);
//// 渲染模板输出
//            //$this->render();
//            return $this->fetch();
//        }
//    }
//layui form表单
    public function index()
    {
        $this->baseindex();
        $where = "u.branch_id=b.id and u.region_id=r.id and u.state =1 ";
        if (isset($_POST['submit'])) {
            //$page=input('get.pageName');
            //dump($page);die;
            $region_id = input('post.region_id');
            $branch_id = input('post.branch_id');
            $ch_name = input('post.ch_name');
            $position = input('post.position');
            if (!empty($region_id) && $region_id != 2) {
                $where .= ' and u.region_id=' . $region_id;
                //dump($where);die;
            }
            if (!empty($branch_id) && $branch_id != 1) {
                $where .= ' and u.branch_id=' . $branch_id;
            }
            if (!empty($ch_name)) {
                $where .= " and `u`.`ch_name` LIKE '%$ch_name%'";
            }
            if (!empty($position)) {
                $where .= " and `u`.`position` LIKE '%$position%'";
            }
        }

        $results = Db::table('oa_user u,oa_branch b, oa_region r')
            ->field('u.id,u.ch_name,b.branch,u.position,r.region')->where($where)->select();
        //dump($list);die;
// 把分页数据赋值给模板变量list
        $this->assign('list', $results);
// 渲染模板输出
        //$this->render();
        return $this->fetch();
    }

    public function staffdel()
    {//删除员工
        $this->baseindex();
        if (isset($_POST['submit']) && $_POST['submit'] == "submit") {
            $id = input('post.id');
            $del = Db::table('oa_user')->where("id=" . $id)->update(['state' => 0]);
            return $del;
        }
    }
}